home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / OpenDoc / OpenDoc Utilities / Interfaces / OrdColl.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  6.4 KB  |  222 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OrdColl.h
  3.  
  4.     Contains:    Definition of class OrderedCollection
  5.  
  6.     Owned by:    Reggie Adkins
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     5/24/96    jpa        Fixed header.
  13.          <2>     5/24/96    jpa        1339104: Made methods nonvirtual/inline for
  14.                                     efficiency (EditorSetup)
  15.  
  16.     To Do:
  17. */
  18.  
  19. #ifndef _ORDCOLL_
  20. #define _ORDCOLL_
  21.  
  22. #ifndef _ODTYPES_
  23. #include "ODTypes.h"
  24. #endif
  25.  
  26. #ifndef _PLFMDEF_
  27. #include "PlfmDef.h"
  28. #endif
  29.  
  30. #ifndef _LINKLIST_
  31. #include "LinkList.h"
  32. #endif
  33.  
  34. #ifndef _ODMEMORY_
  35. #include "ODMemory.h"
  36. #endif
  37.  
  38. //==============================================================================
  39. // Theory of Operation
  40. //==============================================================================
  41.  
  42. // OrdereCollection is an ordered collection of elements of type void* (since
  43. // we can't use templates)
  44. // Duplicates are allowed.
  45.  
  46. //==============================================================================
  47. // Constants
  48. //==============================================================================
  49.  
  50. //==============================================================================
  51. // Scalar Types
  52. //==============================================================================
  53.  
  54. typedef void* ElementType;
  55.  
  56. //=====================================================================================
  57. // Classes defined in this interface
  58. //=====================================================================================
  59.  
  60. class OrderedCollection;    // An ordered (not sorted) collection of ElementTypes
  61. class OrderedCollectionIterator;
  62.  
  63. //=====================================================================================
  64. // Classes used by this interface
  65. //=====================================================================================
  66.  
  67. class ValueLink;             // A link plus a value of type ElementType.
  68.  
  69. //=====================================================================================
  70. // Global Variables
  71. //=====================================================================================
  72.  
  73. //=====================================================================================
  74. // Class ValueLink - Definition
  75. //=====================================================================================
  76.  
  77. class ValueLink : public Link {
  78.     
  79. public:
  80.                 ValueLink(ElementType value)    { fValue = value;}    
  81.     virtual        ~ValueLink()                    { }
  82.     ElementType    GetValue()                        { return fValue;}
  83.     void        SetValue(ElementType v)            { fValue = v;}
  84.  
  85. private:
  86.     ElementType         fValue;
  87. };
  88.  
  89. //=====================================================================================
  90. // Class OrderedCollection
  91. //=====================================================================================
  92.  
  93. class OrderedCollection
  94. {
  95.     
  96. public:
  97.  
  98.     OrderedCollection();
  99.     OrderedCollection(ODMemoryHeapID where);
  100.     virtual ~OrderedCollection();
  101.  
  102.     ODULong Count() const             { return fImplementation.Count(); };
  103.     
  104.     void    AddFirst(ElementType element);
  105.     void    AddLast(ElementType element);
  106.     void    AddBefore(ElementType existing, ElementType tobeadded);
  107.     void    AddAfter(ElementType existing, ElementType tobeadded);
  108.  
  109.     ElementType    After(ElementType existing) const;
  110.     ElementType    Before(ElementType existing) const;
  111.  
  112.     ElementType    First() const;
  113.         // Returns kODNULL if there is no first element.
  114.     ElementType    Last() const;
  115.  
  116.     ElementType    RemoveFirst();
  117.         // Don't call if there are no elements. Crash will result.
  118.     ElementType    RemoveLast();
  119.     void    RemoveAll()                {fImplementation.DeleteAllLinks();}
  120.     
  121.         // Called from the destructor. Removes all elements, deleting the links
  122.         // Does not delete the elements themselves
  123.         
  124.     void    DeleteAll();
  125.     
  126.         // Removes and deletes all elements
  127.         
  128.     ODBoolean    Remove(ElementType existing);
  129.         // Returns true if existing was actually removed.
  130.         
  131.     ODBoolean    Contains(ElementType existing) const;
  132.     
  133.     OrderedCollectionIterator* CreateIterator();
  134.     
  135.     inline ODMemoryHeapID GetHeap() const;
  136.  
  137. protected:
  138.      ValueLink*     CreateNewLink(ElementType value) const;
  139.      virtual ODBoolean    ElementsMatch(ElementType v1,ElementType v2) const;
  140.          // Does a pointer comparison by default. Subclasses may override.
  141.  
  142. private:
  143.     LinkedList        fImplementation;
  144.     ODMemoryHeapID    fHeap; // if kODNULL, use default heap.
  145.  
  146.     friend class OrderedCollectionIterator;
  147.     friend class ListIterator;
  148. };
  149.  
  150. inline ODMemoryHeapID OrderedCollection::GetHeap() const 
  151. {
  152.     return fHeap;
  153. }
  154.  
  155.  
  156. //=====================================================================================
  157. // Class OrderedCollectionIterator
  158. //=====================================================================================
  159.  
  160. class OrderedCollectionIterator {
  161. public:
  162.     OrderedCollectionIterator(OrderedCollection* collection);
  163.     ~OrderedCollectionIterator()    { }
  164.     inline ElementType First();
  165.     inline ElementType Next();
  166.     inline ElementType Last();
  167.     inline ElementType Previous();
  168.     ODBoolean   IsNotComplete()        {return fImplementation.IsNotComplete();}
  169.     void        RemoveCurrent()        {fImplementation.RemoveCurrent();}
  170.     
  171. private:
  172.       OrderedCollection*    fCollection;
  173.     LinkedListIterator    fImplementation;
  174. };
  175.  
  176.  
  177.  
  178. //------------------------------------------------------------------------------
  179. // OrderedCollectionIterator::First
  180. //------------------------------------------------------------------------------
  181.  
  182. ElementType    OrderedCollectionIterator::First()
  183. {
  184.     ValueLink* link = (ValueLink*) fImplementation.First();
  185.     
  186.     return link ? link->GetValue() : (ElementType)kODNULL;
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190. // OrderedCollectionIterator::Next
  191. //------------------------------------------------------------------------------
  192.  
  193. ElementType    OrderedCollectionIterator::Next()
  194. {        
  195.     ValueLink* link = (ValueLink*) fImplementation.Next();
  196.     
  197.     return link ? link->GetValue() : (ElementType)kODNULL;
  198. }
  199.  
  200. //------------------------------------------------------------------------------
  201. // OrderedCollectionIterator::Last
  202. //------------------------------------------------------------------------------
  203.  
  204. ElementType    OrderedCollectionIterator::Last()
  205. {
  206.     ValueLink* link = (ValueLink*) fImplementation.Last();
  207.     
  208.     return link ? link->GetValue() : (ElementType)kODNULL;
  209. }
  210.  
  211. //------------------------------------------------------------------------------
  212. // OrderedCollectionIterator::Previous
  213. //------------------------------------------------------------------------------
  214.  
  215. ElementType    OrderedCollectionIterator::Previous()
  216. {
  217.     ValueLink* link = (ValueLink*) fImplementation.Previous();
  218.     
  219.     return link ? link->GetValue() : (ElementType)kODNULL;
  220. }
  221.  
  222. #endif // _ORDCOLL_